package com.tea.servlet;

import com.tea.entity.Product;
import com.tea.service.ProductService;
import com.tea.service.impl.ProductServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet({
    "/admin/product/add",
    "/admin/product/delete",
    "/admin/product"
})
public class AdminProductServlet extends HttpServlet {
    private final ProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getRequestURI();
        HttpSession session = req.getSession();
        if (session.getAttribute("admin") == null) {
            resp.sendRedirect("/admin-login.jsp");
            return;
        }

        if (pathInfo.contains("/delete")) {
            handleDelete(req, resp);
        } else {
            // 显示产品列表
try {
    req.setAttribute("products", productService.getAllProducts());
} catch (Exception e) {
    req.setAttribute("errorMsg", "获取茶品列表失败：" + e.getMessage());
}
            req.getRequestDispatcher("/admin-dashboard.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        if (session.getAttribute("admin") == null) {
            resp.sendRedirect("/admin-login.jsp");
            return;
        }

        String action = req.getParameter("action");
        if ("add".equals(action)) {
            addProduct(req, resp);
        } else if ("delete".equals(action)) {
            deleteProduct(req, resp);
        }
    }

    private void addProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Product product = new Product();
            product.setName(req.getParameter("name"));
            product.setPrice(Double.parseDouble(req.getParameter("price")));
            product.setDescription(req.getParameter("description"));
            product.setCategoryId(Long.parseLong(req.getParameter("categoryId")));
            product.setMainImage(req.getParameter("mainImage"));

            boolean success = productService.addProduct(product);
            if (success) {
                resp.sendRedirect("/admin-dashboard.jsp?success=添加茶品成功");
            } else {
                req.setAttribute("errorMsg", "添加茶品失败");
                req.getRequestDispatcher("/admin-product-add.jsp").forward(req, resp);
            }
        } catch (Exception e) {
            req.setAttribute("errorMsg", "添加茶品失败：" + e.getMessage());
            req.getRequestDispatcher("/admin-product-add.jsp").forward(req, resp);
        }
    }

    private void deleteProduct(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            int productId = Integer.parseInt(req.getParameter("id"));
            boolean success = productService.deleteProduct(productId);
            resp.sendRedirect("/admin-dashboard.jsp");
        } catch (Exception e) {
            req.setAttribute("errorMsg", "删除茶品失败：" + e.getMessage());
            req.getRequestDispatcher("/admin-dashboard.jsp").forward(req, resp);
        }
    }

    private void handleDelete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        try {
            int productId = Integer.parseInt(req.getParameter("id"));
            boolean success = productService.deleteProduct(productId);
            resp.sendRedirect("/admin-dashboard.jsp");
        } catch (Exception e) {
            try {
                req.setAttribute("errorMsg", "删除失败：" + e.getMessage());
                req.getRequestDispatcher("/admin-dashboard.jsp").forward(req, resp);
            } catch (ServletException ex) {
                ex.printStackTrace();
            }
        }
    }
}